<?php
/**
 * ClassName: baseService
 * Description: service 基类
 * @Author 周昌
 * @Create 2023/11/7 16:37
 */

namespace service;


use libs\Tool;
use model\AdminModel;
use model\CardCateModel;
use model\CardModel;
use service\admin\HospitalService;
use think\Db;
use think\model\Collection;
use wangshouwei\session\Session;

class BaseService
{
    private static $adminInfo;
    public static $status=[0=>'禁用',1=>'启用'];
    public static $status_index=['no'=>0,'yes'=>1];
    public function __construct(){

    }

    //获取等了用户信息
    public static function getAdminInfo(){

        if(!self::$adminInfo){
            self::$adminInfo = AdminModel::getInfo(['id'=>Session::get('user_id')]);
            self::$adminInfo['hospital_group_id'] = HospitalService::getGroupId(self::$adminInfo['hospital_id']);

        }
        return self::$adminInfo;
    }
    //获取医院id
    public static function getUserHospitalId(){
        return self::getAdminInfo()['hospital_id'];
    }
    //获取医院组id
    public static function getUserHospitalGroupId(){
        return self::getAdminInfo()['hospital_group_id'];
    }
    public static function getInfoById($id){
        return static::$model::where('id',$id)->find();
    }
    public static function getInfo($where){
        return static::$model::where($where)->find();
    }
    public static function getList($where){
        return static::$model::where($where)->select()->toArray();
    }
    //通用添加并返回id
    public static function add($data)
    {
        $id = static::$model::strict(false)->insertGetId($data);
        if ($id) {
            return Tool::Success('添加成功',['id'=>$id]);
        }
        return  Tool::Error('添加失败');
    }
    //通用修改
    public static function edit($id,$data)
    {

        $result = static::$model::where('id', $id)->update($data);
        if ($result !== false) {
            return  Tool::Success('修改成功');
        }
        return  Tool::Error('修改失败');
    }
    //通用删除
    public static function del($id)
    {
        $result=static::$model::where('id',$id)->delete();
        if ($result !== false) {
            return  Tool::Success('删除成功');
        }
        return  Tool::Error('删除失败');
    }
    //通用数据列表
    public static function ajaxData($where,$field='*',$order='id desc',$table=null)
    {
        if(empty($table)){
            $table = static::$model::field($field)->order($order);
        }
        return static::tableJsonData($table, $where, function ($data) {
//            $data['addtime'] = date('Y-m-d H:i:s', $data['addtime']);
            return $data;
        });
    }
    //通用修改状态
    public static function status($id,$status_code)
    {
        $result=static::$model::where('id',$id)->update(['status'=>$status_code]);
        if ($result !== false) {
            return  Tool::Success('修改成功');
        }
        return  Tool::Error('修改失败');
    }
    //通用排序
    public static function sort($id,$sort_code)
    {
        $result=static::$model::where('id',$id)->update(['sort'=>$sort_code]);
        if ($result !== false) {
            return  Tool::Success('修改成功');
        }
        return  Tool::Error('修改失败');
    }
    //状态选择 通用方法
    public static function statusSelect()
    {
        static $status_select=[];
        if(empty($status_select)){
            foreach (self::$status as $k=>$v){
                $row=[];
                $row['id']=$k;
                $row['name']=$v;
                $status_select[]=$row;
            }
            array_unshift($status_select,['id'=>'','name'=>'-----请选择-----']);
        }
        return $status_select;
    }
    //通用获取下了列表
    public static function getSelect($where='',$field='id,name',$order=null){
        $result=static::$model::getList($where,$field,$order);
        array_unshift($result,['id' => '', 'name' => '-----请选择-----']);
        return $result;
    }
    //通用获取名称方法
    public static function getNameByKey($id=null,$where='',$field='id,name',$key='id',$key_name='key_name')
    {
        static $key_names =[];
        if(!isset($key_names[$key_name])){
             $key_names[$key_name]=static::$model::getListColumn($where,$field,$key);
        }
        return $id?$key_names[$key_name][$id]:$key_names[$key_name];
    }
    //是否vip用户,在整个医院组查询
    public static function isVip($member_id)
    {
        $hospital_group_id=self::$adminInfo['hospital_group_id'];
        $card_cate_ids=CardCateModel::where(['hospital_group_id'=>$hospital_group_id,'type'=>1])
            ->column('id','id');
        if(!$card_cate_ids){
            return false;
        }
        $count=CardModel::where('card_cate_id','in',$card_cate_ids)->where('member_id',$member_id)->count();
        if($count>0){
            return true;
        }
        return  false;
    }
    public static function tableJsonData($table_name_or_query, $where, callable $row_callback = null, callable $callback = null,$return_json = true)
    {
        $query_count = null;
        if (is_string($table_name_or_query)) {
            $query = Db::name($table_name_or_query);
            $query_count = Db::name($table_name_or_query);
            $query->order("id desc");
        } else {
            $query = $table_name_or_query;
            $query_count = clone $table_name_or_query;
        }
        $count = $query_count->where($where)->count();
        $list = $query->where($where)->limit(\epii\server\Args::params("offset"), \epii\server\Args::params("limit"))->select()->toArray();
        if ($list instanceof Collection) {
            $list = $list->all();
        }
        $outdata = ["rows" => $row_callback ? array_map($row_callback, $list) : $list, "total" => $count];
        if ($callback) {
            $outdata['rows'] = $callback($outdata['rows']);
        }
        if($return_json)
            return json_encode($outdata, JSON_UNESCAPED_UNICODE);
        else return $outdata;
    }


}